Imports System
Imports System.Data
Imports System.Data.Common
Imports System.Collections.Generic
Imports Microsoft.Practices.EnterpriseLibrary.Data

Namespace DAL.Servicio
	Public Class Asistencia
        Public Function Insertar(ByVal _Asistencias As List(Of BEL.Servicio.Asistencia)) As System.Boolean
            Dim db As Database = DatabaseFactory.CreateDatabase("ServCons")
            Dim cm As DbCommand = db.GetStoredProcCommand("[SER_I_Asistencia]")
            Using cn As DbConnection = db.CreateConnection()
                cn.Open()
                Dim tx As DbTransaction = cn.BeginTransaction()
                Try
                    For Each _Asistencia As BEL.Servicio.Asistencia In _Asistencias
                        cm.Parameters.Clear()

                        db.AddInParameter(cm, "CO_RESERVA", DbType.Int32, _Asistencia.CO_RESERVA)
                        db.AddInParameter(cm, "FE_ASISTENCIA", DbType.DateTime, _Asistencia.FE_ASISTENCIA)
                        db.AddInParameter(cm, "FE_HORAASISTENCIA", DbType.DateTime, _Asistencia.FE_HORAASISTENCIA)
                        db.AddInParameter(cm, "FL_ASISTENCIA", DbType.Boolean, _Asistencia.FL_ASISTENCIA)

                        Using dr As IDataReader = db.ExecuteReader(cm, tx)
                            dr.Read()
                            _Asistencia.CO_ASISTENCIA = Convert.ToInt16(dr("CO_ASISTENCIA"))
                        End Using
                    Next

                    tx.Commit()
                Catch _ex As Exception
                    tx.Rollback()
                    Throw New Exception(_ex.Message, _ex.InnerException)
                Finally
                    tx.Dispose()
                    cm.Dispose()
                    tx = Nothing
                    cm = Nothing
                    db = Nothing
                End Try
            End Using
            Return True
        End Function
		Public Function Imprimir(ByVal _Parametros As Object()) As DataTable
            Dim ds As New DataSet()
            Dim db As Database = DatabaseFactory.CreateDatabase()
            Dim cm As System.Data.Common.DbCommand = db.GetStoredProcCommand("[SER_S_Asistencia_Reporte]")
            db.AddInParameter(cm, "NO_DOCIDENTIDAD", DbType.String, Convert.ToString(_Parametros(0)))
            db.AddInParameter(cm, "CO_SERVICIO", DbType.Int32, Convert.ToInt32(_Parametros(1)))
            db.AddInParameter(cm, "FE_INICIO", DbType.DateTime, Convert.ToDateTime(_Parametros(2)))
            db.AddInParameter(cm, "FE_FIN", DbType.DateTime, Convert.ToDateTime(_Parametros(3)))
            Try
                ds = db.ExecuteDataSet(cm)
                ds.Tables(0).TableName = "REPORTE"
                Return ds.Tables(0)
            Catch ex As System.Data.SqlClient.SqlException
                Throw New Exception(ex.Message, ex.InnerException)
            End Try
        End Function
        Public Function ListarReservasActivas(ByVal _Parametros As Object()) As DataTable
            Dim ds As New DataSet()
            Dim db As Database = DatabaseFactory.CreateDatabase()
            Dim cm As System.Data.Common.DbCommand = db.GetStoredProcCommand("[SER_S_Reserva_Asistencia_Listado]")
            db.AddInParameter(cm, "CO_SERVICIO", DbType.Int32, Convert.ToInt32(_Parametros(0)))
            db.AddInParameter(cm, "CO_NATURAL", DbType.Int32, Convert.ToInt32(_Parametros(1)))
            db.AddInParameter(cm, "FL_SEXO", DbType.String, Convert.ToString(_Parametros(2)))

            Try
                ds = db.ExecuteDataSet(cm)
                ds.Tables(0).TableName = "SERVICIOS"
                Return ds.Tables(0)
            Catch ex As System.Data.SqlClient.SqlException
                Throw New Exception(ex.Message, ex.InnerException)
            End Try
        End Function
        Public Function ListarReservas(ByVal _Parametros As Object()) As DataTable
            Dim ds As New DataSet()
            Dim db As Database = DatabaseFactory.CreateDatabase()
            Dim cm As System.Data.Common.DbCommand = db.GetStoredProcCommand("[SER_S_Reserva_Asistencia]")
            db.AddInParameter(cm, "CO_PLAN", DbType.Int32, Convert.ToInt32(_Parametros(0)))
            db.AddInParameter(cm, "CO_SERVICIO", DbType.Int32, Convert.ToInt32(_Parametros(1)))
            db.AddInParameter(cm, "CO_ITEM", DbType.Int32, Convert.ToInt32(_Parametros(2)))
            db.AddInParameter(cm, "CO_NATURAL", DbType.Int32, Convert.ToInt32(_Parametros(3)))

            Try
                ds = db.ExecuteDataSet(cm)
                ds.Tables(0).TableName = "RESERVAS"
                Return ds.Tables(0)
            Catch ex As System.Data.SqlClient.SqlException
                Throw New Exception(ex.Message, ex.InnerException)
            End Try
        End Function
	End Class
End Namespace